package com.runen.wnhz.runen.presenter.iPresenter;

import android.text.TextUtils;

import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.RechargeBean;
import com.runen.wnhz.runen.presenter.BasePresenter;
import com.runen.wnhz.runen.presenter.Contart.PayContract;
import com.runen.wnhz.runen.presenter.model.PayModel;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 描述：
 * 作者： 天天童话丶
 * 时间： 2018/6/2.
 */

public class IPayPersenter extends BasePresenter<PayModel, PayContract.View> {

    @Inject
    public IPayPersenter(PayModel mModel, PayContract.View mView) {
        super(mModel, mView);
    }

    /*
    * 充值
    * */
    public void reqeustRecharge() {
        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("type", mView.getPayChannel());
        if (TextUtils.equals(mView.getRechargeType(), "1")) {
            map.put("amount", mView.getAmount());
        } else {
            map.put("rid", mView.getRid());
        }

        mModel.rechargeApi(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<RechargeBean>>() {
                    @Override
                    public void call(BaseEntity<RechargeBean> rechargeBean) {
                        switch (rechargeBean.getRe()) {
                            case 0:
                                mView.onError(rechargeBean.getInfo());
                                break;
                            case 1:
                                mView.onSuccess(rechargeBean.getData());
                                break;
                            case -1:
                                mView.onTokenError(rechargeBean.getInfo());
                                break;
                            default:
                                mView.onError(rechargeBean.getInfo());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.onError(throwable.getMessage().toString());
                    }
                });
    }


    /*
   *  支付
   * */
    public void reqeustPay() {
        Map<String, String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("type", mView.getPayChannel());
        map.put("order_no", mView.getOrderNo());

        mModel.payApi(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<RechargeBean>>() {
                    @Override
                    public void call(BaseEntity<RechargeBean> rechargeBean) {
                        switch (rechargeBean.getRe()) {
                            case 0:
                                mView.onError(rechargeBean.getInfo());
                                break;
                            case 1:
                                mView.onSuccess(rechargeBean.getData());
                                break;
                            case -1:
                                mView.onTokenError(rechargeBean.getInfo());
                                break;
                            default:
                                mView.onError(rechargeBean.getInfo());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.onError(throwable.getMessage().toString());
                    }
                });
    }
}

